import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:city_pickers/city_pickers.dart';
import 'package:city_pickers/modal/result.dart';
import 'package:common_utils/common_utils.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_switch/flutter_switch.dart';
import 'package:taonile/config/config.dart';
import 'package:taonile/utils/singleton_manager.dart';

import '../entity/order_detail_entity.dart';
import '../entity/order_list_entity.dart';
import '../entity/refund_reason_entity.dart';
import '../http/api.dart';
import '../routers/routers.dart';
import '../utils/common_utils.dart';
import '../views/dialog_manager.dart';

///
/// 订单退换服务选择
///
class OrderRefundChoosePage extends StatefulWidget {
  var orderNo = '';
  var goodsId = '';
  var img = '';
  var name = '';
  var spec = '';
  var price = '';
  var num = '';

  OrderRefundChoosePage(
      {Key? key,
      this.orderNo = '',
      this.goodsId = '',
      this.img = '',
      this.name = '',
      this.spec = '',
      this.price = '',
      this.num = ''})
      : super(key: key);

  @override
  State<OrderRefundChoosePage> createState() => _OrderRefundChoosePageState();
}

class _OrderRefundChoosePageState extends State<OrderRefundChoosePage> {
  @override
  void initState() {
    super.initState();
    returnReason();
    buReason();
  } //退货的得原因，缓存到本地

  returnReason() async {
    if (SingletonManager.instance.returnReason.isNotEmpty) {
      LogUtil.v('退货退款原因列表已经缓存好了');
      return;
    }
    dynamic result = await Api.drawbackList('0');
    var jsonResult = jsonDecode(jsonEncode(result));
    RefundReasonEntity entity = RefundReasonEntity.fromJson(jsonResult);
    SingletonManager.instance.returnReason = entity.data;
  }

  //补发的得原因，缓存到本地
  buReason() async {
    if (SingletonManager.instance.buReason.isNotEmpty) {
      LogUtil.v('补发原因列表已经缓存好了');
      return;
    }
    dynamic result = await Api.drawbackList('1');
    var jsonResult = jsonDecode(jsonEncode(result));
    RefundReasonEntity entity = RefundReasonEntity.fromJson(jsonResult);
    SingletonManager.instance.buReason = entity.data;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Config.colorBg2,
        appBar: AppBar(
          title: const Text('选择服务'),
          centerTitle: true,
        ),
        body: Container(
          width: double.infinity,
          height: double.infinity,
          margin: EdgeInsets.symmetric(vertical: 30.w, horizontal: 30.w),
          child: SingleChildScrollView(
            child: Column(
              children: [
                Container(
                  decoration: BoxDecoration(
                    color: Config.colorWhite,
                    borderRadius: BorderRadius.circular(10.w),
                  ),
                  width: double.infinity,
                  padding: EdgeInsets.only(left: 25.w, right: 25.w, top: 20.w),
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Expanded(
                            child: Text(
                              '退换商品',
                              style: TextStyle(fontSize: 26.sp),
                            ),
                          ),
                        ],
                      ),
                      Divider(
                        color: Config.colorDivider,
                        height: 40.w,
                        thickness: 2.w,
                      ),
                      Container(
                        padding: EdgeInsets.only(top: 8.w),
                        height: 140.w,
                        decoration: BoxDecoration(
                          border:
                              Border(bottom: BorderSide(color: Config.colorDivider, width: 1.w)),
                        ),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Container(
                              width: 100.w,
                              height: 100.w,
                              margin: EdgeInsets.only(right: 20.w),
                              child: ClipRRect(
                                borderRadius: BorderRadius.circular(5),
                                child: CachedNetworkImage(
                                  imageUrl: widget.img,
                                  fit: BoxFit.fill,
                                  width: 100.w,
                                  height: 100.w,
                                  errorWidget: (context, url, error) {
                                    return Image.asset(
                                      'assets/images/bg_default_400.png',
                                      width: 100.w,
                                      height: 100.w,
                                      fit: BoxFit.fill,
                                    );
                                  },
                                ),
                              ),
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Container(
                                  width: 520.w,
                                  child: Text(
                                    widget.name,
                                    softWrap: false,
                                    style: TextStyle(color: (Config.colorText1), fontSize: 22.w),
                                  ),
                                ),
                                Container(
                                  width: 520.w,
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(
                                        widget.spec,
                                        softWrap: true,
                                        style:
                                            TextStyle(color: (Config.colorText1), fontSize: 20.w),
                                      ),
                                      Text(
                                        "X${widget.num}",
                                        softWrap: true,
                                        style:
                                            TextStyle(color: (Config.colorText1), fontSize: 20.w),
                                      ),
                                    ],
                                  ),
                                ),
                                Padding(
                                  padding: EdgeInsets.only(top: 9.w),
                                ),
                                Text(
                                  '${widget.price}金券',
                                  softWrap: true,
                                  style: TextStyle(color: (Config.colorText1), fontSize: 20.w),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  decoration: BoxDecoration(
                    color: Config.colorWhite,
                    borderRadius: BorderRadius.circular(10.w),
                  ),
                  width: double.infinity,
                  margin: EdgeInsets.only(top: 25.w),
                  padding: EdgeInsets.only(left: 25.w, right: 25.w, top: 20.w, bottom: 20.w),
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Expanded(
                            child: Text(
                              '选择服务类型',
                              style: TextStyle(fontSize: 26.sp),
                            ),
                          ),
                        ],
                      ),
                      Divider(
                        color: Config.colorDivider,
                        height: 40.w,
                        thickness: 2.w,
                      ),
                      InkWell(
                        onTap: () {
                          Routes.navigateTo(context, Routes.orderApplyRefund, params: {
                            'type': '1',
                            'orderNo': widget.orderNo,
                            'goodsId': widget.goodsId,
                            'img': widget.img,
                            'name': widget.name,
                            'spec': widget.spec,
                            'price': widget.price,
                            'num': widget.num,
                          }).then((value) {
                            if (value != null && value['result'] == 1) {
                              Routes.router.pop(context, {'result': 1});
                            }
                          });
                        },
                        child: Container(
                          height: 85.w,
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Icon(
                                Icons.settings_backup_restore,
                                size: 38.w,
                              ),
                              Padding(padding: EdgeInsets.only(left: 20.w)),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text(
                                    '我要退款(无需退货)',
                                    style: TextStyle(fontSize: 26.sp),
                                  ),
                                  Padding(padding: EdgeInsets.only(top: 5.w)),
                                  Text(
                                    '没收到货',
                                    style: TextStyle(fontSize: 23.sp, color: Config.colorText3),
                                  ),
                                ],
                              ),
                              Expanded(
                                child: Container(
                                  alignment: Alignment.centerRight,
                                  child: Icon(
                                    Icons.arrow_forward_ios,
                                    size: 26.w,
                                    color: Config.colorText3,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                      Divider(
                        color: Config.colorDivider,
                        height: 40.w,
                        thickness: 2.w,
                      ),
                      InkWell(
                        onTap: () {
                          Routes.navigateTo(context, Routes.orderApplyRefund, params: {
                            'type': '2',
                            'orderNo': widget.orderNo,
                            'goodsId': widget.goodsId,
                            'img': widget.img,
                            'name': widget.name,
                            'spec': widget.spec,
                            'price': widget.price,
                            'num': widget.num,
                          }).then((value) {
                            if (value != null && value['result'] == 1) {
                              Routes.router.pop(context, {'result': 1});
                            }
                          });
                        },
                        child: Container(
                          height: 85.w,
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Icon(
                                Icons.party_mode_outlined,
                                size: 38.w,
                              ),
                              Padding(padding: EdgeInsets.only(left: 20.w)),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text(
                                    '我要退货退款',
                                    style: TextStyle(fontSize: 26.sp),
                                  ),
                                  Padding(padding: EdgeInsets.only(top: 5.w)),
                                  Text(
                                    '已收到货，需要退还收到的货物',
                                    style: TextStyle(fontSize: 23.sp, color: Config.colorText3),
                                  ),
                                ],
                              ),
                              Expanded(
                                child: Container(
                                  alignment: Alignment.centerRight,
                                  child: Icon(
                                    Icons.arrow_forward_ios,
                                    size: 26.w,
                                    color: Config.colorText3,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                      Divider(
                        color: Config.colorDivider,
                        height: 40.w,
                        thickness: 2.w,
                      ),
                      InkWell(
                        onTap: () {
                          Routes.navigateTo(context, Routes.orderApplyRefund, params: {
                            'type': '3',
                            'orderNo': widget.orderNo,
                            'goodsId': widget.goodsId,
                            'img': widget.img,
                            'name': widget.name,
                            'spec': widget.spec,
                            'price': widget.price,
                            'num': widget.num,
                          }).then((value) {
                            if (value != null && value['result'] == 1) {
                              Routes.router.pop(context, {'result': 1});
                            }
                          });
                        },
                        child: Container(
                          height: 85.w,
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Icon(
                                Icons.send_outlined,
                                size: 38.w,
                              ),
                              Padding(padding: EdgeInsets.only(left: 20.w)),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text(
                                    '补货商品',
                                    style: TextStyle(fontSize: 26.sp),
                                  ),
                                  Padding(padding: EdgeInsets.only(top: 5.w)),
                                  Text(
                                    '已收到货，卖家少发商品',
                                    style: TextStyle(fontSize: 23.sp, color: Config.colorText3),
                                  ),
                                ],
                              ),
                              Expanded(
                                child: Container(
                                  alignment: Alignment.centerRight,
                                  child: Icon(
                                    Icons.arrow_forward_ios,
                                    size: 26.w,
                                    color: Config.colorText3,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ));
  }
}
